/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Etbl_Locations;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MrKoi
 */
public class DALtbl_Locations extends BaseConnection{
    CallableStatement clbStm=null;
    ResultSet rs=null;
      public boolean addtbl_Locations(Etbl_Locations obj){
        try {
            getConnection();
            clbStm=cnn.prepareCall("{call tbl_Locations_add(?,?,?)}");
            clbStm.setInt("cityID", obj.getCityId());
            clbStm.setString("name",obj.getName());
            clbStm.setString("descriptions",obj.getDescriptions());            
            clbStm.execute();
            clbStm.close();
            releaseConnection();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Locations.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
      }
      
      public boolean edittbl_Locations(Etbl_Locations obj){
        try {
            getConnection();
            clbStm=cnn.prepareCall("{call tbl_Locations_update(?,?,?,?)}");
            clbStm.setInt("locations_Id",obj.getLocations_Id());
            clbStm.setInt("cityID", obj.getCityId());
            clbStm.setString("name",obj.getName());
            clbStm.setString("descriptions",obj.getDescriptions());           
            clbStm.execute();
            clbStm.close();
            releaseConnection();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Locations.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
      }
      
      public boolean deletetbl_Locations(int locations_Id){
        try {
            getConnection();
            clbStm=cnn.prepareCall("{call tbl_Locations_delete(?)}");
            clbStm.setInt("locations_Id",locations_Id);              
            clbStm.execute();
            clbStm.close();
            releaseConnection();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Locations.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
      }
      
      public Etbl_Locations getOnetbl_Locations(int locations_Id){
          Etbl_Locations obj=new Etbl_Locations();
          try {
            getConnection();
            clbStm=cnn.prepareCall("{call tbl_Locations_get(?)}");
            clbStm.setInt("locations_Id",locations_Id);              
            rs=clbStm.executeQuery();            
            while(rs.next()){                            
                obj.setLocations_Id(rs.getInt("locations_Id"));
                obj.setName(rs.getString("name"));
                obj.setDescriptions(rs.getString("desciptions"));
                obj.setCityId(rs.getInt("cityId"));
                obj.setCity_Name(rs.getString("city_name"));                
            }   
            clbStm.close();
            rs.close();
            releaseConnection();
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Locations.class.getName()).log(Level.SEVERE, null, ex);
            
        }finally{
              return obj;
          }
      }
      
      public ArrayList getListLocations(String orderBy, String ASCorDESC, String WhereClause, int PagingIndex, int PagingSize){
        try {
            ArrayList LocaltionsList=new ArrayList();
            getConnection();
            clbStm=cnn.prepareCall("{call tbl_Locations_getpaged(?,?,?,?,?)}");
            clbStm.setString("OrderBy", orderBy);
            clbStm.setString("TangGiam", ASCorDESC);
            clbStm.setString("WhereClause", WhereClause);
            clbStm.setInt("PageIndex", PagingIndex);
            clbStm.setInt("PageSize", PagingSize);
            rs=clbStm.executeQuery();
            DALTotalPageIndex.getTotalPage("tbl_Locations", WhereClause, PagingSize);
            while(rs.next()){
                Etbl_Locations obj=new Etbl_Locations();
                obj.setLocations_Id(rs.getInt("locations_Id"));
                obj.setName(rs.getString("name"));
                obj.setDescriptions(rs.getString("descriptions"));
                obj.setCityId(rs.getInt("cityId"));
                obj.setCity_Name(rs.getString("city_name")); 
                LocaltionsList.add(obj);
            }
            clbStm.close();
            rs.close();
            releaseConnection();
            return LocaltionsList;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Locations.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
